package com.zklh.leetcode.string;

/**
 * 最长回文字符串
 *
 * @author 坐看落花
 * @version 1.0
 * @date 2021/4/12 15:57
 */
public class LongestPalindromeString {

    volatile boolean a = false;
    /**
     * 最长回文字符串
     * @param str
     * @return
     */
    public String longestPalindrome(String str) {
        String res = "";
        String res1 = "";
        String res2 = "";
        for (int i = 0; i < str.length(); i++) {
            res1 = longestPalindrome(str, i,i);
            res2 = longestPalindrome(str, i,i+1);
            res = res1.length() > res.length() ? res1 : res;
            res = res2.length() > res.length() ? res2 : res;
            System.out.println(11);
        }
        return res;
    }

    /**
     * ij 是回文串比较的中心点, i,j可能是同一个
     * @param str
     * @param i
     * @param j
     * @return
     */
    private String longestPalindrome(String str, int i, int j) {
        for(;i>=0&&j<str.length()&&str.charAt(i) == str.charAt(j);) {
            i--;
            j++;
        }
        i=i+2;
        j=j-1;
        if (i > j) {
            return "";
        }
        return str.substring( i, j);
    }

    public static void main(String[] args) {
        LongestPalindromeString longestPalindromeString = new LongestPalindromeString();
        System.out.println(longestPalindromeString.longestPalindrome("st2bb"));
    }

}
